package com.controller;

import com.entity.Cart;
import com.service.CartService;
import com.util.StatusCode;
import com.vo.LayuiPageVo;
import com.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  购物车控制器
 * </p>
 *
 * @author LMN KCT
 * @since 2021-7-13
 */
@Controller
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 商品详情界面：加购商品
     * 前端传入加购操作（cartoperate：加购）,获取session中用户id信息，判断是否登录
     * (1). 加购商品
     * 1.前端传入商品用户id（userid）、商品id（commid）、商品名（commname）
     *   商品所在农场（school）
     * 2.session中获取购物车用户id（userid）
     * 3.进行加购操作
     */
    @ResponseBody
    @PostMapping("/cart/operate")
    public ResultVo insertcart(@RequestBody Cart cart, HttpSession session) {
        String userid = (String) session.getAttribute("userid");
        Integer cartoperate = cart.getCartoperate();
        cart.setUserid(userid);

        if (StringUtils.isEmpty(userid)) {
            return new ResultVo(false, StatusCode.ACCESSERROR, "请先登录");
        }
        if (cartoperate == 1) {
            Integer i = cartService.insertCart(cart);
            if(i==1){
                return new ResultVo(true, StatusCode.OK, "加购成功");
            }
            return new ResultVo(false, StatusCode.ERROR, "加购失败");

        }else {
            Cart cart1 = cartService.queryCartStatus(cart);
            /**判断是否为本人操作*/
            if (cart1.getUserid().equals(userid)){
                Integer i = cartService.updateCart(cart);
                if (i == 1){
                    return new ResultVo(true, StatusCode.OK,"删除成功");
                }
                return new ResultVo(false,StatusCode.ERROR,"删除失败");
            }
            return new ResultVo(false,StatusCode.ACCESSERROR,"禁止操作");
        }
    }

    /**
     * 购物车列表界面取消加购
     * 1.前端传入购物车id（id）
     * 2.判断是否本人取消加购
     * 3.进行取消加购操作
     */
    @ResponseBody
    @PutMapping("/cart/delete/{id}")
    public ResultVo deletecart(@PathVariable("id") String id, HttpSession session){
        String userid = (String) session.getAttribute("userid");
        Cart cart = new Cart().setId(id).setUserid(userid);
        Cart cart1 = cartService.queryCartStatus(cart);
        /**判断是否为本人操作*/
        if (cart1.getUserid().equals(userid)){
            cart.setCartoperate(2);
            Integer i = cartService.deleteFromCart(cart);
            if (i == 1){
                return new ResultVo(true, StatusCode.OK,"删除成功");
            }
            return new ResultVo(false,StatusCode.ERROR,"删除失败");
        }
        return new ResultVo(false,StatusCode.ACCESSERROR,"禁止操作");
    }

    /**
     * 分页查看用户所有购物车内容
     * 前端传入页码、分页数量
     * 查询分页数据
     */
    @ResponseBody
    @GetMapping("/user/cart/queryall")
    public LayuiPageVo usercart(int limit, int page, HttpSession session) {
        String userid = (String) session.getAttribute("userid");
        List<Cart> cartList = cartService.queryAllCart((page - 1) * limit, limit, userid);
        Integer dataNumber = cartService.queryCartCount(userid);
        return new LayuiPageVo("",0,dataNumber,cartList);
    }

    /**
     * 增加数量
     */
    @ResponseBody
    @PutMapping("/cart/add/{id}")
    public ResultVo add(@PathVariable("id") String id, HttpSession session){
        String userid = (String) session.getAttribute("userid");
        Cart cart = new Cart().setId(id).setUserid(userid);
        Cart cart1 = cartService.queryCartStatus(cart);
        /**判断是否为本人操作*/
        if (cart1.getUserid().equals(userid)){
            Integer i = cartService.addCart(cart);
            if (i == 1){
                return new ResultVo(true, StatusCode.OK,"增加成功");
            }
            return new ResultVo(false,StatusCode.ERROR,"增加失败");
        }
        return new ResultVo(false,StatusCode.ACCESSERROR,"禁止操作");
    }
    /**
     * 减少数量
     */
    @ResponseBody
    @PutMapping("/cart/decrease/{id}")
    public ResultVo decrease(@PathVariable("id") String id, HttpSession session){
        String userid = (String) session.getAttribute("userid");
        Cart cart = new Cart().setId(id).setUserid(userid);
        Cart cart1 = cartService.queryCartStatus(cart);
        /**判断是否为本人操作*/
        if (cart1.getUserid().equals(userid)){
            Integer i = cartService.decreaseCart(cart);
            if (i == 1){
                return new ResultVo(true, StatusCode.OK,"减少成功");
            }
            return new ResultVo(false,StatusCode.ERROR,"减少失败");
        }
        return new ResultVo(false,StatusCode.ACCESSERROR,"禁止操作");
    }
}

